<?php
declare(strict_types=1);
// hook public_function_before.php
/**
 * @param $proArr [1,20,100，4000] 4121计算概率获得
 * @return int|string
 */
function get_rand($proArr)
{
    $result = false;
    //概率数组的总概率精度
    $proSum = array_sum($proArr);
    if (empty($proSum)) {
        return false;
    }
    //概率数组循环
    mt_srand();
    foreach ($proArr as $key => $proCur) {
        $randNum = mt_rand(1, $proSum);
        if (!empty($proCur) && $randNum <= $proCur) {
            //if ( $randNum <= $proCur) {
            $result = $key;
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);
    return $result;
}

function unid()
{
    //return bcmul(getut(),100000,0);
    [$usec, $sec] = explode(" ", microtime());
    return bcmul(bcadd(date('YmdHis', (int)$sec), $usec, 6), '1000000', 0);
}

function gunid()
{
    return bcmul(get_ut().'', '100000', 0);
}

function xn_rand($n = 16)
{
    mt_srand();
    $str = '0123456789abcdefghijklmnopqrstuvwxyz';
    $len = strlen($str);
    $return = '';
    for ($i = 0; $i < $n; $i++) {
        $r = mt_rand(1, $len);
        $return .= $str[$r - 1];
    }
    return $return;
}

function mid($n, $min, $max)
{
    if ($n < $min) return $min;
    if ($n > $max) return $max;
    return $n;
}

function isMobile($mobile)
{
    return preg_match('#^1[\d]{10}$#', $mobile) ? true : false;
}

function isMac($mac_address)
{
    return preg_match('#^[A-F0-9]{2}(-[A-F0-9]{2}){5}$#', $mac_address) ? true : false;
}
function appVersion($version)
{
    return (int)str_ireplace('.', '',$version);
}

function CheckSubstrs($substrs, $text)
{
    foreach ($substrs as $substr)
        if (false !== strpos($text, $substr)) {
            return true;
        }
    return false;
}

/**
 * 签名算法
 * $appKey = 'test';
 * $appSecret = 'test';
 * $sessionkey= 'test';
 * //参数数组
 * $paramArr = array(
 * 'app_key' => $appKey,
 * 'session_key' => $sessionkey,
 * 'method' => 'taobao.user.seller.get',
 * 'format' => 'json',
 * 'v' => '2.0',
 * 'sign_method'=>'md5',
 * 'timestamp' => date('Y-m-d H:i:s'),
 * );
 *
 * //生成签名
 * $sign = createSign($paramArr);
 * //组织参数
 * $strParam = createStrParam($paramArr);
 * $strParam .= 'sign='.$sign;
 */
function createSign($paramArr, $appSecret)
{
    $sign = $appSecret;
    ksort($paramArr);
    foreach ($paramArr as $key => $val) {
        if ($key != '' && $val != '') {
            $sign .= $key . $val;
        }
    }
    $sign .= $appSecret;
    $sign = strtoupper(md5($sign));
    return $sign;
}

function createSign_echo($paramArr, $appSecret)
{
    $sign = '<b style="color: #28a3ef">' . $appSecret . '</b>';
    ksort($paramArr);
    foreach ($paramArr as $key => $val) {
        if ($key != '' && $val != '') {
            $sign .= '<b style="color: #f92672">' . $key . '</b>' . $val;
        }
    }
    $sign .= '<b style="color: #28a3ef">' . $appSecret . '</b>';
    //$sign = strtoupper(md5($sign));
    return $sign;
}

//组参函数

function createStrParam($paramArr)
{

    $strParam = '';
    foreach ($paramArr as $key => $val) {
        if ($key != '' && $val != '') {
            $strParam .= $key . '=' . urlencode($val) . '&';
        }
    }
    return $strParam;
}


function pagination_tpl($url, $text, $active = '')
{
    $g_pagination_tpl = $active ? $active : '<li><a href="{url}">{text}</a></li>';
    return str_replace(['{url}', '{text}'], [$url, $text, $active], $g_pagination_tpl);
}

function pagination(string $url,int $totalnum,int $page,int $pagesize = 20)
{
    $totalpage = ceil($totalnum / $pagesize);
    if ($totalpage < 2) return '';
    $page = min($totalpage, $page);
    $shownum = 2;    // 显示多少个页 * 2

    $start = max(1, $page - $shownum);
    $end = min($totalpage, $page + $shownum);

    // 不足 $shownum，补全左右两侧
    $right = $page + $shownum - $totalpage;
    $right > 0 && $start = max(1, $start -= $right);
    $left = $page - $shownum;
    $left < 0 && $end = min($totalpage, $end -= $left);

    $s = '';
    $page != 1 && $s .= pagination_tpl(str_replace('{page}', ''.($page - 1), $url), '上一页', '');
    if ($start > 1) $s .= pagination_tpl(str_replace('{page}', '1', $url), '1 ' . ($start > 2 ? '...' : ''));
    for ($i = $start; $i <= $end; $i++) {
        $s .= pagination_tpl(str_replace('{page}', ''.$i, $url), ''.$i, $i == $page ? ' <li class="active"><span>'.$i.'</span></li>' : '');
    }

    if ($end != $totalpage) $s .= pagination_tpl(str_replace('{page}', $totalpage.'', $url), ($totalpage - $end > 1 ? '...' : '') . $totalpage);
    $page != $totalpage && $s .= pagination_tpl(str_replace('{page}', ''.($page + 1), $url), '下一页');
    return $s;
}

// 简单的上一页，下一页，比较省资源，不用count(), 推荐使用，命名与 bootstrap 保持一致
function pager($url, $totalnum, $page, $pagesize = 20)
{
    $totalpage = ceil($totalnum / $pagesize);
    if ($totalpage < 2) return '';
    $page = min($totalpage, $page);

    $s = '';
    $page > 1 and $s .= '<li><a href="' . str_replace('{page}', ''.($page - 1), $url) . '">上一页</a></li>';
    $s .= " $page / $totalpage ";
    $totalnum >= $pagesize and $page != $totalpage and $s .= '<li><a href="' . str_replace('{page}', ''.($page + 1), $url) . '">下一页</a></li>';
    return $s;
}

function getImg($key, $domain = '')
{
    $upload_domain = _CONF('upload_domain');
    $upload_domain = $upload_domain ? 'http:' . $upload_domain : '';
    !empty($domain) && $upload_domain = $domain;
    return rtrim($upload_domain, '/') . '/' . $key;
}


function en_scale($num, $scale = 100, $size = 0)
{
    empty($num) && $num="0";
    return bcmul($num, $scale, $size);
}

function dc_scale($num, $scale = 0.01, $size = 2)
{
    empty($num) && $num="0";
    return bcmul($num, $scale, $size);
}


//验证身份证
function is_idcard($id)
{
    $id = strtoupper($id);
    $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
    $arr_split = array();
    if (!preg_match($regx, $id)) {
        return FALSE;
    }
    if (!isset($id[16])) //检查15位
    {
        $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
        preg_match($regx, $id, $arr_split);
        //检查生日日期是否正确
        $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) {
            return FALSE;
        } else {
            return TRUE;
        }
    } else {
        $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
        preg_match($regx, $id, $arr_split);
        $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) {
            return FALSE;
        } else {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sign = 0;
            for ($i = 0; $i < 17; $i++) {
                $b = (int)$id[$i];
                $w = $arr_int[$i];
                $sign += $b * $w;
            }
            $n = $sign % 11;
            $val_num = $arr_ch[$n];
            if ($val_num != substr($id, 17, 1)) {
                return FALSE;
            } else {
                return TRUE;
            }
        }
    }
}

// hook public_function_after.php

?>